package com.ruoyi.pay.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.pay.domain.WxpayConfig;
import com.ruoyi.pay.mapper.WxpayConfigMapper;
import com.wechat.pay.java.core.RSAPublicKeyConfig;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Configuration
public class WxPayAutoConfig {

    @Resource
    private WxpayConfigMapper wxpayConfigMapper;

    @PostConstruct
    public RSAPublicKeyConfig rsaAutoCertificateConfig() {
        QueryWrapper<WxpayConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("is_check", 0);
        WxpayConfig wxPayConfig = wxpayConfigMapper.selectOne(wrapper);
        // 这里把Config作为配置Bean是为了避免多次创建资源，一般项目运行的时候这些东西都确定了
        // 具体的参数改为申请的数据，可以通过读配置文件的形式获取
         RSAPublicKeyConfig config = new RSAPublicKeyConfig.Builder()
                .merchantId(wxPayConfig.getMerchantId())
                .privateKey(wxPayConfig.getPrivateKey())
                //原来是获取文件，改为上面代码直接获取
                .privateKey(wxPayConfig.getPrivateKey())
                 .publicKey(wxPayConfig.getPublicKey())
                 .publicKeyId(wxPayConfig.getPublicKeyId())
                .merchantSerialNumber(wxPayConfig.getMerchantSerialNumber())
                .apiV3Key(wxPayConfig.getApiV3key())
                .build();
        return config;
    }
}
